package com.zzh.demo.core.common.utils;

import java.util.Base64;

import static com.zzh.demo.core.common.constants.Constants.BASIC_AUTHORIZE_PREFIX;


/**
 * description： <br>
 * createTime: 2018/2/269:58 <br>
 *
 * @author zzh
 */
public class BasicAuthorizeUtils {

    /**
     *
     * 方法名称：basic验证
     * 方法描述: <br>
     *
     * @param   basicString 请求头里的basic认证信息
     * @param   user 请求头里的basic认证的user
     * @param   password 请求头里的basic认证的password
     * @return  验证结果
     * <p>创建人：周志辉
     * <p>创建时间：2018/1/4 15:36
     */
    public static boolean basicAuthorize(String basicString, String user, String password) {
        if(basicString != null && basicString.startsWith(BASIC_AUTHORIZE_PREFIX)) {
            String suffix = basicString.substring(BASIC_AUTHORIZE_PREFIX.length(), basicString.length());
            String[] array = new String(Base64.getDecoder().decode(suffix)).split(":");
            if(array.length == 2 && array[0].equals(user) && array[1].equals(password)) {
                return true;
            }
        }
        return false;
    }
}
